package com.pega.tulokhorecord.data.database;

import java.sql.SQLException;
import java.util.ArrayList;

import android.content.Context;

import com.pega.tulokhorecord.data.SetInfo;
import com.pega.tulokhorecord.data.TurnInfo;

/**
 * @author phapli
 * 
 */
public class DatabaseManager {
	static private DatabaseManager instance;
	static public boolean DATABASE_DEBUG = false;

	static public DatabaseManager getInstance(Context ctx) {
		if (null == instance) {
			instance = new DatabaseManager(ctx);
		}
		return instance;
	}

	private DatabaseHelper helper;

	private DatabaseManager(Context ctx) {
		helper = new DatabaseHelper(ctx);
	}

	private DatabaseHelper getHelper() {
		return helper;
	}

	/******************************************
	 * **************************************** Function with table Ebook
	 * *****************************************
	 ******************************************/

	public int addSetRecord(SetInfo record) {
		try {
			getHelper().getSetInfoDao().create(record);
		} catch (Exception e) {
			return -1;
		}
		return record.get_id();
	}

	public SetInfo getSetRecordWithId(int id) {
		try {
			return getHelper().getSetInfoDao().queryForId(id);
		} catch (Exception e) {
			return null;
		}
	}

	public ArrayList<SetInfo> getAllSet() {
		ArrayList<SetInfo> setInfos = new ArrayList<SetInfo>();
		try {
			setInfos = (ArrayList<SetInfo>) getHelper().getSetInfoDao()
					.queryForAll();
		} catch (SQLException e) {
			// e.printStackTrace();
			return null;
		}
		return setInfos;
	}

	/******************************************
	 * **************************************** Function with table TurnInfo
	 * *****************************************
	 ******************************************/

	public int addTurnRecord(TurnInfo record) {
		try {
			getHelper().getTurnInfoDao().create(record);
		} catch (Exception e) {
			return -1;
		}
		return record.get_id();
	}

	public int updateTurnInfoRecord(TurnInfo record) {
		try {
			getHelper().getTurnInfoDao().update(record);
		} catch (Exception e) {
			return -1;
		}
		return record.get_id();
	}

	public TurnInfo getTurnInfoWithId(int id) {
		try {
			return getHelper().getTurnInfoDao().queryForId(id);
		} catch (Exception e) {
			return null;
		}
	}

	public int updateEbook(SetInfo record) {
		try {
			getHelper().getSetInfoDao().update(record);
		} catch (Exception e) {
			return -1;
		}
		return record.get_id();
	}

}
